<%@ page contentType="text/html;charset=UTF-8" %>
<%@ include file="/WEB-INF/views/include/taglib.jsp"%>
<html>
<head>
    <title>录入</title>
</head>

<script>

    /*诊疗项目补全*/
    autoCompleteClinic("itemText");
    function autoCompleteClinic(inputId) {
        var url = "${ctx}/oper/clinicItemDict/automatic?itemClass=E,J,K";
        autoComplete(inputId, url,
                function (data) {
                    var rows = [];
                    for (var i = 0; i < data.length; i++) {
                        rows[rows.length] = {
                            data: data[i],
                            value: data[i].id,
                            result: data[i].itemName
                        };
                    }
                    return rows;
                }, function (data, i, max) {
                    return data.itemName + "|   " + data.itemCode + "|   " + data.price+"元";
                }, function (event, data, formatted) {
                    var td = "<tr><td>"+data.itemName+"</td>" +
                            "<td style='display: none'>"+data.itemClass+"</td>" +
                            "<td style='display: none'>"+data.id+"</td>" +
                            "<td>"+data.expand2+"</td>" +
                            "<td>"+data.price+"</td>" +
                            "<td>打折</td>" +
                            "<td>1</td>" +
                            "<td>"+data.price+"</td>" +
                            "<td style='display: none'>"+data.performedBy+"</td>" +
                            "<td>是</td>" +
                            "<td><input type='hidden' value='0'>非收费</td>" +
                            "<td><input type='hidden' value='1'> <a href='#' onclick='deleteItem(this)' class='applyBt btn btn-danger btn-xs'>删除</a></td>" +
                            "</tr>"
                    var table = $("#itemTable");
                    table.append(td);
                    $("#itemText").val("");
                })
        ;
    }





    var itemClass ="";
    $(function (){
        //检查
        $("#examType").change(function(){
            var parentId = $(this).children('option:selected').val();
            if(parentId == "" || parentId == null){
                return false;
            }
            itemClass= "D";
            itemSubSelect(parentId,'','${ctx}/exam/examApply/queryByPId?parent.id='+parentId);
        })
    })
        //检验
        $("#labType").change(function(){
            var parentId = $(this).children('option:selected').val();
            if(parentId == "" || parentId == null){
                return false;
            }
            itemClass= "C";
            itemSubSelect(parentId,'','${ctx}/lab/labApply/queryBylabTypeId?id=' + parentId)
    })
    //套餐
    $("#comoboName").change(function(){
        var parentId = $(this).children('option:selected').val();
        if(parentId == "" || parentId == null){
            return false;
        }
        comoboSub(parentId,'${ctx}/sys/titemcomobo/tItemComobo/queryByPId?id=' + parentId)
    })


    function comoboSub(parentId,url){
        var html = '<div id="comSelected">';
        html+= '<dl><dt>套餐项目</dt></dl></div>';
        $('#drag_h').html('<b>需要 </b><span onclick="exam.confirm2(\'1\')">确定</span><span onclick="exam.confirm2(\'0\')">取消</span>');
        $('#drag_con').html(html);
        exam.ShowComo(parentId,url);
        boxAlpha();
        draglayer();
    }

    //检查、检验 弹出框
    function itemSubSelect(parentId,clinicItemIds,url){
        var dragHtml ='<div id="examDivId" class="drag-main">';		//检查项目
        dragHtml+='		<dl id="examSelected"  class="drag-selected"><dt>已选项目：</dt><dd class="drag-main-div"></dd></dl>';
        dragHtml+='</div>';
        $('#drag_h').html('<b>请选择检查项目</b><span onclick="exam.confirm()">确定</span>');
        $('#drag_con').html(dragHtml);
        exam.Show(parentId,clinicItemIds,url);
        boxAlpha();
        draglayer();
    }

    var exam = {
        into:function(clinicItemIds){
            if(clinicItemIds == "" || clinicItemIds == null){
                return false;
            }
            var strs= new Array(); //定义一数组
            strs=clinicItemIds.split(","); //字符分割
            for (var i=0;i<strs.length ;i++ )
            {
                exam.Chk(''+strs[i]+'');
            }
        },
        Show:function(parentId,clinicItemIds,url){
            $.ajax({
                type:"post",
                url:url,
                dataType: "json",
                success:function(data){
                    if (data.length > 0) {
                        for(var i=0;i<data.length;i++){
                            var examHtml="";
                            examHtml+='<div id="maincity'+i+'">';
                            examHtml+='<h4>'+data[i].name+':</h4>';
                            examHtml+='<dl style="height:50px;"><dt>&nbsp;</dt><dd class="drag-main-div">';
                            if(data[i].clinicItemDictList!=null){
                                for(var j=0;j<data[i].clinicItemDictList.length;j++) {
                                    examHtml+='<li class="selected" style="height:auto;" performedBy="'+data[i].clinicItemDictList[j].orgDept.id+'" deptName="'+data[i].clinicItemDictList[j].orgDept.deptName+'"  price="'+data[i].clinicItemDictList[j].clinicItemDict.price+'" id="'+data[i].clinicItemDictList[j].clinicItemDict.id+'" onclick="exam.Chk(\''+data[i].clinicItemDictList[j].clinicItemDict.id+'\')">'+data[i].clinicItemDictList[j].clinicItemDict.itemName+'</li>';
                                }
                            }
                            examHtml+='</dd></dl>';
                            examHtml+='</div>';
                            $("#examSelected").after(examHtml);
                        }
                        if(clinicItemIds!="" && clinicItemIds!=null){
                            exam.init(clinicItemIds);
                        }
                    }
                }
            })
        },
        ShowComo:function(parentId,url){
            $.ajax({
                type:"post",
                url:url,
                dataType: "json",
                success:function(data) {
                    if (data.length > 0) {
                        for (var i = 0; i < data.length; i++) {
                            var htmlCom = "";
                            var dept = "";
                            if(data[i].deptName == 'undefined' || data[i].deptName ==undefined ){
                                dept = "";
                            }else{
                                var dept =data[i].deptName;
                            }
                            htmlCom += '<dt>&nbsp;</dt><dd class="drag-main-div">';
                            htmlCom += '<li  style="height:auto;" performedBy="' + data[i].performedBy + '"itemClass="' + data[i].itemClass + '" coefficient="' + data[i].coefficient+ '" deptName="' + data[i].deptName + '" price="' + data[i].price + '" id="' + data[i].clinicId+ '"itemName="' + data[i].itemName + '">' + data[i].itemName + '<span style="margin: 0px 20px 0px 20px;">金额:'+data[i].price+'元</span>          执行科室:'+dept+'</li>';
                            htmlCom += '</dd>';
                            $("#comSelected").append(htmlCom);
                        }

                    }
                }

            })
        },
        Chk: function(id){
            var isChk= $("#"+id).attr("class");
            if(isChk.indexOf("chkON")!=-1){
                $("#chk"+id).remove();
                $("#"+id).attr("class","selected");
                return false;
            }
            $("#"+id).addClass("chkON");
            var chkHtml=$("#"+id).clone(true);
            var liXm=$(chkHtml).attr("id",id);
            $("#examSelected dd").append(liXm);
            $("#chk"+id).attr("onclick","exam.del('chk"+id+"')");
            $("#chk"+id).attr("chkId",id);

        },
        del: function(id) {
            var chkId=$("#"+id).attr("chkId");
            $("#"+id).remove();
            $("#"+chkId).attr("class","selected");
        },
        confirm:function(){
            $("#examSelected dd li").each(function(index,domEle){
              var itemName =$(this).html();
              var deptName = $(this).attr("deptname");
              var itemId = $(this).attr("id");
              var  deptId = $(this).attr("performedBy");
              var price = $(this).attr("price");
              var coefficient = $(this).attr("coefficient");
              var td = "<tr><td>"+itemName+"</td>" +
                      "<td style='display: none'>"+itemClass+"</td>" +
                      "<td style='display: none'>"+itemId+"</td>" +
                      "<td>"+deptName+"</td>" +
                      "<td>"+price+"</td>" +
                      "<td>打折</td>" +
                      "<td>1</td>" +
                      "<td>"+price+"</td>" +
                      "<td style='display: none'>"+deptId+"</td>" +
                      "<td>是</td>" +
                      "<td><input type='hidden' value='0'>否</td>" +
                      "<td><input type='hidden' value='1'><a href='#' onclick='deleteItem(this)' class='applyBt btn btn-danger btn-xs'>删除</a></td>" +
                      "</tr>"
                var table = $("#itemTable");
                table.append(td);
            })
            boxAlpha();
        },
        confirm2:function(type){
            if(type =="0"){
                boxAlpha();
                return false;
            } else{
                $("#comSelected dd li").each(function(index,domEle){
                    var itemName =$(this).attr("itemName");
                    var deptName = $(this).attr("deptname");
                    var itemClass = $(this).attr("itemClass");
                    var  deptId = $(this).attr("performedBy");
                    var price = $(this).attr("price");
                    var itemId = $(this).attr("id");
                    var coefficient = $(this).attr("coefficient");
                    var td = "<tr><td>"+itemName+"</td>" +
                            "<td style='display: none'>"+itemClass+"</td>" +
                            "<td style='display: none'>"+itemId+"</td>" +
                            "<td>"+deptName+"</td>" +
                            "<td>"+price+"</td>" +
                            "<td>打折</td>" +
                            "<td>"+coefficient+"</td>" +
                            "<td>"+price+"</td>" +
                            "<td style='display: none'>"+deptId+"</td>" +
                            "<td>否</td>" +
                            "<td><input type='hidden' value='0'>未收费</td>" +
                            "<td> <input type='hidden' value='1'><a href='#' onclick='deleteItem(this)' class='applyBt btn btn-danger btn-xs'>删除</a></td>" +
                            "</tr>"
                    var table = $("#itemTable");
                    table.append(td);
                })
                boxAlpha();
            }
        }
    }


    function deleteItem(tr){
        $(tr).parent().parent().remove();
    }
    function saveItem(patientId,clinicId){
        var rows = $("#itemTable").find("tbody tr");
        console.log(rows.length)
        if(rows.length >0){
            var text = "";
            for(var i=0;i<rows.length;i++){
                var row = rows.eq(i).find("td");
                var itemName = row.eq(0).text();//项目名称
                var itemClass = row.eq(1).text();//项目类别
                var itemId = row.eq(2).text();//项目id
                var deptName = row.eq(3).text();//执行科室
                var price = row.eq(4).text();//价格
                var Zl = row.eq(6).text();//折率
                var implementDept = row.eq(8).text();//执行科室id
                var chargeFlag = row.eq(10).find('input').val();//是否收费
                var isNow = row.eq(11).find('input').val();//是否收费

                var sumPrice = Number(price * Zl);//总价
                text+="<tr><td>" +
                "<input type='hidden' name='outpTreatRecs["+i+"].itemClass'"+" value='"+itemClass+"'/>" +
                "<input type='hidden' name='outpTreatRecs["+i+"].implementDept'"+" value='"+implementDept+"'/>" +
                "<input type='hidden' name='outpTreatRecs["+i+"].itemName'"+" value='"+itemName+"'/>" +
                "<input type='hidden' name='outpTreatRecs["+i+"].price'"+" value='"+price+"'/>" +
                "<input type='hidden' name='outpTreatRecs["+i+"].num'"+" value='1'/>" +
                "<input type='hidden' name='outpTreatRecs["+i+"].sumPrice'"+" value='"+sumPrice+"'/>" +
                "<input type='hidden' name='outpTreatRecs["+i+"].clinicItemId'"+" value='"+itemId+"'/>" +
                "<input type='hidden' name='outpTreatRecs["+i+"].chargeFlag'"+" value='"+chargeFlag+"'/>" +
                "<input type='hidden' name='outpTreatRecs["+i+"].patientId'"+" value='"+patientId+"'/>" +
                "<input type='hidden' name='outpTreatRecs["+i+"].clinicId'"+" value='"+clinicId+"'/>" +
                "<input type='hidden' name='outpTreatRecs["+i+"].isNow'"+" value='"+isNow+"'/>" +
                "</td></tr>";
            }
            var list = "<table>"+text+"</table>";
            $("#itemTableForm").html(list);

            var divUrl= "${ctx}/physical/itemInput?patientId="+patientId+"&clinicId="+clinicId;
            $("#itemTableForm").ajaxSubmit({
                type: 'post', // 提交方式 get/post
                url:'${ctx}/physical/savePhyItem', // 需要提交的 url
                success: function(data) { // data 保存提交后返回的数据，一般为 json 数据
                    if (data.code == "success") {
                        toastr.success(data.data);
                        loadDiv("rigthDoctorCenterDiv", divUrl);
                    } else if(data.code == 'info'){
                        loadDiv("rigthDoctorCenterDiv", divUrl);
                    } else {
                        flag = true;
                        toastr.error(data.data);
                    }
                }
            });
        } else{
            toastr.error("请选择提交项目后再保存");
            return false;
        }

    }

</script>
<body>
<form id="itemTableForm">

</form>
<div class="panel-body">
    <div class="datatable-top">
        <div class="pull-left">
            <form:form id="searchForm"  modelAttribute="outpTreatRec" onsubmit="return loadDivForm('phyUserRight','searchForm','${ctx}/physical/addphyUser');" method="post" class="form-inline">
                <input id="pageNo" name="pageNo" type="hidden" value="${page.pageNo}"/>
                <input id="pageSize" name="pageSize" type="hidden" value="${page.pageSize}"/>
                &nbsp;&nbsp;<label>检查 ：</label>
                <form:select path="" id="examType"  data-parsley-required="true" class="form-control" onchange="">
                    <form:option value="" label="请选择"></form:option>
                    <form:options items="${examTypeList}" itemLabel="name" itemValue="id" htmlEscape="false"/>
                </form:select>
                &nbsp;&nbsp;<label>检验：</label>
                <form:select path="" id="labType"  data-parsley-required="true"  class="form-control" onchange="">
                    <form:option value="" label="请选择"></form:option>
                    <form:options items="${labTypeList}" itemLabel="name" itemValue="id" htmlEscape="false"/>
                </form:select>
                &nbsp;&nbsp;<label>项目选择:</label>
                    <input id="itemText" data-parsley-required="true" htmlEscape="false" class="form-control"  maxlength="64" />
                &nbsp;&nbsp;<label>套餐名称:</label>
                <form:select path="" id="comoboName"  data-parsley-required="true" class="form-control" onchange="">
                    <form:option value="" label="请选择"></form:option>
                    <form:options items="${tItemComoboList}" itemLabel="name" itemValue="id" htmlEscape="false"/>
                </form:select>
                <input id="btnSubmit" class="btn btn-primary" onclick="saveItem('${outpTreatRec.patientId}','${outpTreatRec.clinicId}')" type="button" value="保存"/>
            </form:form>
        </div>
    </div>
    <table id="itemTable" class="table table-bordered mg-t datatable" style="width: 100%">
        <thead>
        <tr>
            <th>项目名称</th>
            <th style="display: none">项目类型</th>
            <th style="display: none">项目Id</th>
            <th>科室</th>
            <th>原价</th>
            <th>优惠方式</th>
            <th>折率</th>
            <th>价格(元)</th>
            <th style="display: none">科室Id</th>
            <th>是否附加</th>
            <th>是否收费</th>
            <th>操作</th>
        </tr>
        </thead>
        <tbody>

        <c:forEach items="${page.list}" var="list">
        <tr onclick="loadFormData()">
           <td>${list.itemName}</td>
           <td style="display: none"></td>
           <td style="display: none"></td>
           <td>${list.implementDeptEt.deptName}</td>
           <td>${list.sumPrice}</td>
           <td>打折</td>
           <td>1</td>
           <td>${list.sumPrice}</td>
           <td style="display: none">${list.implementDept}</td>
           <td><input type="hidden" value="${list.itemComoboName}"><c:choose>
                    <c:when test="${list.itemComoboName eq '' ||  list.itemComoboName eq null}">
                        是
                    </c:when>
                   <c:otherwise>
                       否
                   </c:otherwise>
           </c:choose></td>
           <td><input type="hidden" value="${list.chargeFlag}">${fns:getDictLabels(list.chargeFlag,'NO_CHARGE','')}</td>
           <td></td>
        </tr>
        </c:forEach>
    </table>
    <div class="pagination">${page}</div>
</div>

</body>
</html>
